package com.jd.myaaaccount;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jd.myaaaccount.entity.Family;
import com.jd.myaaaccount.entity.Settle;
import com.jd.myaaaccount.model.Common;
import com.jd.myaaaccount.model.DayBookModel;
import com.jd.myaaaccount.model.FamilyModel;
import com.jd.myaaaccount.util.AlertDialogHelper;
import com.jd.myaaaccount.util.ErrorCode;
import com.jd.myaaaccount.util.OperateCode;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class SettlePage extends BasePage {

	private ImageView ivFilter;
	private ImageView ivRefresh;
	private TextView tvStartDate;
	private TextView tvEndDate;
	private TextView tvSettleMoney;
	private ListView lvDetail;

	private FamilyModel fm;
	private DayBookModel dm;
	private Handler handler;

	List<Family> families;
	String[] familiesName;
	int selectedFamilyIndex;

	private Settle settle;

	@Override
	public void iniPage(Activity context) {
		super.iniPage(context);

		fm = new FamilyModel(context);
		dm = new DayBookModel(context);

		iniControl();

		handler = new Handler() {
			@Override
			public void handleMessage(android.os.Message msg) {

				switch (msg.what) {
				case OperateCode.GET_FAMILY:
					if (msg.arg1 == ErrorCode.SUCCESS) {
						families = (List<Family>) msg.obj;
						familiesName = new String[families.size()];
						for (int i = 0; i < families.size(); i++) {
							familiesName[i] = families.get(i).getName();
						}
						if (families.size() > 0) {
							selectedFamilyIndex = 0;
							refreshSettle();
						}
					} else {
						Common.showErrorInfo(SettlePage.this.context, msg.arg1,
								"", 0);
					}
					break;
				case OperateCode.STATISTIC:// 显示统计结果
					pd.cancel();
					if (msg.arg1 == ErrorCode.SUCCESS) {
						settle = (Settle) msg.obj;
						showSettle();
					} else {
						Common.showErrorInfo(SettlePage.this.context, msg.arg1,
								"加载失败", 0);
					}
					break;
				default:
					break;
				}
			};
		};

		iniFamily();
	}

	private void iniControl() {

		tvStartDate = (TextView) context.findViewById(R.id.tvStartDate);
		tvEndDate = (TextView) context.findViewById(R.id.tvEndDate);
		tvSettleMoney = (TextView) context.findViewById(R.id.tvSettleMoney);
		lvDetail = (ListView) context.findViewById(R.id.lvDetail);

		ivFilter.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {

				Builder builder = new AlertDialog.Builder(
						SettlePage.this.context);
				builder.setTitle("家庭");
				builder.setSingleChoiceItems(familiesName, 0,

				new OnClickListener() {

					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						selectedFamilyIndex = arg1;
					}
				});
				builder.setPositiveButton("确定",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface arg0, int arg1) {
								refreshSettle();
							}
						});
				builder.create().show();
			}
		});

		ivRefresh.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				refreshSettle();
			}
		});
	}

	/**
	 * 初始化家庭
	 */
	private void iniFamily() {

		// pd = AlertDialogHelper.showWaitProgressDialog(context, "加载中…");
		new Thread(new Runnable() {
			@Override
			public void run() {
				fm.getMyFamily(handler);
			}
		}).start();
	}

	@SuppressLint("SimpleDateFormat")
	private void showSettle() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		tvStartDate.setText(sdf.format(settle.getStartDate()));
		tvEndDate.setText(sdf.format(settle.getEndDate()));
		tvSettleMoney.setText(String.valueOf(settle.getMoney()));

		List<Map<String, Object>> detailMapList = new ArrayList<Map<String, Object>>();
		for (Settle.SettleDetail item : settle.getSettleDetails()) {
			Map<String, Object> detailMap = new HashMap<String, Object>();
			detailMap.put("consumer", item.getUser().getName());
			detailMap.put("paymoney", item.getPay());
			detailMap.put("consummoney", item.getConsume());
			detailMap.put("settlemoney", item.getSettle());
			detailMapList.add(detailMap);
		}

		SimpleAdapter sa = new SimpleAdapter(context, detailMapList,
				R.layout.layout_settle_detail_list_item, new String[] {
						"paymoney", "consummoney", "settlemoney", "consumer" },
				new int[] { R.id.tvPayMoney, R.id.tvConsumeMoney,
						R.id.tvSettleMoney, R.id.tvConsumer });
		lvDetail.setAdapter(sa);
	}

	private void refreshSettle() {
		pd = AlertDialogHelper.showWaitProgressDialog(context, "加载中…");
		new Thread(new Runnable() {
			@Override
			public void run() {
				dm.statistic(families.get(selectedFamilyIndex).getId(), handler);
			}
		}).start();
	}

	private void saveSettle() {

	}
}
